package com.example.tet;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView tv;
	private long start;
	private long end;
	private boolean flag = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv = (TextView) findViewById(R.id.show);
		tv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if (flag) {
					pause();
				}else{
					start();
				}
				
			}
		});
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			tv.setText(toTime(System.currentTimeMillis()));
		}
	};

	public class MyThread implements Runnable {
		@Override
		public void run() {
			while (flag) {
				try {
					Thread.sleep(1);// 线程暂停10秒，单位毫秒
					Message message = handler.obtainMessage();
					message.what = 1;
					handler.sendMessage(message);// 发送消息
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void pause() {
		flag = false;
	}

	public void start() {
		start = System.currentTimeMillis();
		flag = true;
		new Thread(new MyThread()).start();
	}

	private String toTime(long start) {
		long millisecond = start % 1000;
//		millisecond /= 100;
		start /= 1000;
		long minute = start / 60;
		long second = start % 60;
		minute %= 60;
		return String.format("%02d:%02d:%03d", minute, second, millisecond);
	}
}


